package com.tron.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Slf4j
@Api(tags = "外标题")
public class FeignProviderController extends BaseController {

    @Value("${server.port}")
    private String port;

    @ApiOperation(value = "内标题",notes = "详情")
    /**
     * 演示该接口方法需要带用户权限信息，返回请求头中的信息
     */
    @GetMapping("/test01")
    public Object test01() {
        //获取请求中携带的token
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader("X-Authorization-access_token");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return buildResult("来自feign-provider: "+port+"服务的数据：请求头信息--->"+token);
    }

    /**
     * 演示get请求路径传参，返回接收到的参数
     * @param a
     * @return
     */
    @GetMapping("/test02")
    public Object test02(@RequestParam String a) {
        return buildResult("来自feign-provider: "+port+"服务的数据：方法传参--->"+a);
    }

    /**
     * 演示get请求路径传参，返回接收到的参数
     * @param a
     * @param b
     * @return
     */
    @GetMapping("/test03/{a}")
    public Object test03(@PathVariable String a, @RequestParam String b) {
        return buildResult(String.format("来自feign-provider: "+port+"服务的数据：方法传参--->a=%1$s,b=%2$s",a,b));
    }

    /**
     * 演示get请求用对象接收参数，log打印参数，并返回一个异常信息
     * @param myParam
     * @return
     */
    @GetMapping("/test04")
    public Object test04(MyParam myParam) {
        log.info("接收的参数为："+myParam);
        if (!myParam.getC()) {
            throw new RuntimeException("来自feign-provider: "+port+"服务的异常信息：抛个异常玩玩");
        }
        return buildResult(myParam);
    }

    /**
     * 演示post请求传参，返回接收到的参数
     * @param myParam
     * @return
     */
    @PostMapping("/test05")
    public Object test05(@RequestBody MyParam myParam) {
        return buildResult(myParam);
    }

    @GetMapping("/sleep3")
    public Object test06() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return buildResult("来自feign-provider: "+port+"服务的数据：休眠3秒");
    }

    @PostMapping("/fileUpload")
    public JSONObject fileUpload(MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("message", file.getOriginalFilename());
        jsonObject.put("size", file.getSize());
        return jsonObject;
    }

    @GetMapping("/fileDownload")
    public void fileDownload(HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode("外星人统治地球.txt","UTF-8"));
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            outputStream.write("i am tron".getBytes());
        }

    }

}
